package com.example.teacher.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.commonutils.ResultData;
import com.example.teacher.entity.EduCourse;
import com.example.teacher.entity.Teacher;
import com.example.teacher.service.EduCourseService;
import com.example.teacher.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/backstage/indexfront")
public class IndexFrontController {
    @Autowired
    private EduCourseService courseService;
    @Autowired
    private TeacherService teacherService;

    //查询前8的热门课程和前4的名师
    @Cacheable(value = "teacherAndCourse",key="'info'")
    @GetMapping(value = "index",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ResultData index(){
        //查询课程中的前8
        QueryWrapper<EduCourse> wrapper =new QueryWrapper<>();
        wrapper.orderByDesc("view_count");
        wrapper.last("limit 8");
        List<EduCourse> courseList = courseService.list(wrapper);
        //查询讲师中的前4
        QueryWrapper<Teacher> wrapperT =new QueryWrapper<>();
        wrapperT.orderByAsc("sort");
        wrapperT.last("limit 4");
        List<Teacher> teacherList = teacherService.list(wrapperT);
        return ResultData.ok().data("courseList",courseList).data("teacherList",teacherList);
    }
}
